<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新建会员/车辆信息</title>
<%@include file="../include/include.jsp"%>
<%-- <link rel="stylesheet" type="text/css" href="${csspath }/public.css" /> --%>
<script type="text/javascript" src="${layerPath}/jquery/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>
<script type="text/javascript" src="${layerPath}/layer.js"></script>
<script type="text/javascript" src="${layerPath}/extend/layer.ext.js"></script>
</head>
<script type="text/javascript">
	var j = 1;
$(function(){
	var oldMobile = $("#mobilePhone").val();
	var carPlate = "";
	//取消getMaintainMemberPage
	$("#cancel").click(function(){
         window.location.href="${webcontext}/crmdp/dealer/member/redirectMaintainMemberPage";
	});
	
	$("#memberName").focus(function(){
		$('#newName').html("");
	});
	
	$("#updateName").click(function(){
		var memberName = $("#memberName").val();
		memberName.replace(/[ ]/g,"");
		if(memberName == ""){
			jAlert("姓名不能为空", "提示信息");
		}else{
			$.ajax({
				type:'POST',
				url: '${webcontext}/crmdp/dealer/member/updateMemberName',
				contentType : "application/json;charset=utf-8",
				async: true,
				data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
					,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
				dataType : 'json',
				success : function(data) {
					if(data.success){
						$('#newName').html("&nbsp;&nbsp;姓名修改成功！");
						$("#updateName").hide();
						$("#toUpdateName").show();
						document.getElementById("memberName").disabled =true;
					}else{
						$("#updateName").hide();
						$("#toUpdateName").html("修改");
						document.getElementById("memberName").disabled =true;
						jAlert("姓名添加失败！","提示信息");
					}
			    }
			});
		}
	});
	
	$("#toUpdateName").click(function(){
		$('#newName').html("");
		$("#toUpdateName").hide();
		$("#updateName").show();
		$("#updateName").html("确认");
		document.getElementById("memberName").disabled =false;
	});
	
	$("#toUpdateMobile").click(function(){
		
		$.ajax({
			type:'POST',
			url:'${webcontext}/crmdp/dealer/member/ischeckoperationpwd',
			contentType:"application/json;charset=utf-8",
			async: true,
			dataType : 'json',
			success : function(data) {
				if(data){
					//密码文本
					 layer.prompt({title: '输入授权密码',formType: 1,  closeBtn: false}, function(value, index, elem){
						$.ajax({
							type:'POST',
							url: '${webcontext}/login/operationpasswordForUpdateMobile',
							async: false,
							data:"operationPassword=" + value,
							dataType : 'json',
							success : function(data) {
							   if(data.verifyOperationPwd) {
								   	$("#toUpdateMobile").hide();
									$("#updateMobile").html("确认");
									document.getElementById("mobilePhone").disabled =false;
									$('#newMobile').html("");
									layer.close(index);
							   }else{
								  jAlert("密码错误!");
								  layer.close(index);
							   }
						    }
						});
					}); 
					//loadLayer(this, "${webcontext}/login/toOperationPwdForUpdateMobile",500,165);
				}else{
					$("#toUpdateMobile").hide();
					$("#updateMobile").html("确认");
					document.getElementById("mobilePhone").disabled =false;
					$('#newMobile').html("");
				}
		    }
		});
	});
	
	$("#updateMobile").focus(function(){
		$('#newMobile').html("");
	});
	
	$("#updateMobile").click(function(){
		if($("#mobilePhone").val().length != 11){
			jAlert("请输入正确的手机号码！", "提示信息");
			return;
		}else if($("#mobilePhone").val() == oldMobile){
			$("#toUpdateMobile").show();
			$("#updateMobile").html("");
			document.getElementById("mobilePhone").disabled =true;
			return;
		}else{
			$.ajax({
				type:'POST',
				url:'${webcontext}/crmdp/dealer/member/chackMemberMobile',
				contentType:"application/json;charset=utf-8",
				async: true,
				data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
					,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
				dataType : 'json',
				success : function(data) {
					if(data.existMember){
						jConfirm("您修改的手机号已经存在。<br><font style='color: red;font-weight: bolder;'>用户名："+data.memberName+"； 手机号："+data.mobilePhone+"。</font><br>是否将所有已激活套餐合并到该用户下？", 
							"提示信息", function(result){
							if(result == true){
								$.ajax({
									type:'POST',
									url:'${webcontext}/crmdp/dealer/member/mergeMemberInfo',
									contentType:"application/json;charset=utf-8",
									async: true,
									data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
										,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
									dataType : 'json',
									success : function(data) {
										jAlert("您的套餐已合并到用户"+data.memberName+"下，手机号为"+data.mobilePhone+"。<br>请返回后查看。", "提示信息");
								    }
								});
							}
						})
					}else{
						$.ajax({
							type:'POST',
							url:'${webcontext}/crmdp/dealer/member/updateMemberMobile',
							contentType:"application/json;charset=utf-8",
							async: true,
							data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
								,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
							dataType : 'json',
							success : function(data) {
								if(data.success){
									$('#newMobile').html("&nbsp;&nbsp;手机号修改成功！");
								}else{
									jAlert("手机号修改失败！","提示信息");
									$('#newMobile').html("");
								}
						    }
						});
					}
			    }
			});
		}
		$("#toUpdateMobile").show();
		$("#updateMobile").html("");
		document.getElementById("mobilePhone").disabled =true;
	});
	
	$("#add").click(function(){
		var carplate = document.getElementById("carPlate");
		carplate.value = carplate.value.replace(/(^\s*)|(\s*$)/g,"");
		if(carplate.value.length == 0){
			jAlert("请输入车牌号！","提示信息");
		} else{
			$.ajax({
				type:'POST',
				url: '${webcontext}/crmdp/dealer/member/checkCarPlateForThisMember',
				contentType : "application/json;charset=utf-8",
				async: true,
				data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
					,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
				dataType : 'json',
				success : function(data) {
					if(data.carplateOfMemberExist){
						jAlert("该车牌已存在,请重新添加！", "提示信息");
					} else { 
						$.ajax({
							type:'POST',
							url: '${webcontext}/crmdp/dealer/member/checkCarPlateForMember',
							contentType : "application/json;charset=utf-8",
							async: true,
							data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
								,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
							dataType : 'json',
							success : function(data) {
								if(data.carplatesExist){
									jAlert("一个车牌只允许关联一个手机号，该车牌已关联其他手机号，请重新添加！", "提示信息");
								}else{
									$("#add").remove();
									$("#span_msg").html("提交中…");
									$.ajax({
					    				type:'POST',
					    				url: '${webcontext}/crmdp/dealer/member/addCarPlateForMember',
					    				contentType : "application/json;charset=utf-8",
					    				async: true,
					    				data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
					    					,'carPlate':$("#carPlate").val(),'id':$("#memberid").val()}),
					    				dataType : 'json',
					    				success : function(data) {
					    					if(data.success){
					    						window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
					    							+ data.mobilePhone
					    							+ "&memberName="
					    							+ data.memberName
					    							+ "&carPlate="
					    							+ data.carPlate
					    							+ "&id="
					    							+ data.id;
					    					}else{
					    						jAlert("该车牌已存在，请重新添加！","提示信息");
					    					}
					    			    }
					    			});
								}
						    }
						});
					}
				}
			});	
		}
	});
});
     	
		function removeById(o){
			var carPlates = document.getElementsByName("carPlates");
			if(carPlates.length == 1){
				jAlert("至少需要有一个车牌！","提示信息");
				return false;
			}
			$.ajax({
				type:'POST',
				url: '${webcontext}/crmdp/dealer/member/removeCarPlateForMember',
				async: true,
				contentType : "application/json;charset=utf-8",
				data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
					,'carPlate':$("#carPlate").val(),'id':o,'memberid':$("#memberid").val()}),
				dataType : 'json',
				success : function(data) {
					if(data.success){
						window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
							+ data.mobilePhone
							+ "&memberName="+ data.memberName
							+ "&carPlate="+ data.carPlate
							+ "&id="+ data.memberId;
					}else{
						jAlert("车牌删除失败，请重新操作！","提示信息");
					}
			    }
			}); 
		}

		function toUpdateCarplate(o){
			carPlate = $("#"+o).val();
			$("#toUpdate"+o).hide();
			$("#update"+o).html("确认");
			document.getElementById(o).disabled =false;
		}
		
		function toUpdate(o){
			carPlate = $("#"+o).val();
			jConfirm("车牌下有正在使用的套餐，<br><font style='color: red;font-weight: bolder;'>只可修改${memberResultsDto.updateCarplateTimes}次！</font>是否仍然修改？", 
				"提示信息",function(result){
				if(result == true){
					$.ajax({
						type:'POST',
						url: '${webcontext}/crmdp/dealer/member/checkUpdateCarPlateTimes',
						async: true,
						contentType : "application/json;charset=utf-8",
						data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
							,'carPlate':$("#carPlate").val(),'id':o,'memberid':$("#memberid").val()}),
						dataType : 'json',
						success : function(data) {
							if(data.greaterThanUpdateCarplateTimes){
								jAlert("车牌修改次数超过最大限制，不可以修改！","提示信息");
							}else{
								$("#toUpdate"+o).hide();
								$("#update"+o).html("确认");
								document.getElementById(o).disabled =false;
							}
						}
					});
				}
			});
		}
		
		function updateCarplate(o){
			if(carPlate == $("#"+o).val()){
				$("#update"+o).html("");
				$("#toUpdate"+o).show();
				document.getElementById(o).disabled =true;
				return;
			}
			$.ajax({
				type:'POST',
				url: '${webcontext}/crmdp/dealer/member/checkCarPlateForMember',
				contentType : "application/json;charset=utf-8",
				async: true,
				data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
					,'carPlate':$("#"+o).val(),'id':$("#memberid").val()}),
				dataType : 'json',
				success : function(data) {
					if(data.carplatesExist){
						jAlert("该车牌已存在,请重新修改！", "提示信息",function(){
							window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
								+ data.mobilePhone
								+ "&memberName="+ data.memberName
								+ "&carPlate="+ data.carPlate
								+ "&id="+ data.id;
						});
					}else{
						$.ajax({
							type:'POST',
							url: '${webcontext}/crmdp/dealer/member/updateCarPlateWithoutPackage',
							async: true,
							contentType : "application/json;charset=utf-8",
							data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
								,'carPlate':$("#"+o).val(),'id':o,'memberid':$("#memberid").val()}),
							dataType : 'json',
							success : function(data) {
								if(data.updateCarplateSuccess){
									jAlert("车牌修改成功！","提示信息",function(){
									window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
										+ data.mobilePhone
										+ "&memberName="+ data.memberName
										+ "&carPlate="+ data.carPlate
										+ "&id="+ data.memberId;
									});
								}else{
									jAlert("车牌修改失败，请重新操作！","提示信息",function(){
										window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
											+ data.mobilePhone
											+ "&memberName="+ data.memberName
											+ "&carPlate="+ data.carPlate
											+ "&id="+ data.memberId;
									});
								}
						    }
						});
					}
					}
			});
		}
		
		function update(o){
			if(carPlate == $("#"+o).val()){
				$("#update"+o).html("");
				$("#toUpdate"+o).show();
				document.getElementById(o).disabled =true;
				return;
			}
			if($.trim($("#"+o).val()).length == 0){
				jAlert("车牌不能为空！","提示信息");
				return;
			}
			$.ajax({
				type : 'POST',
				url : '${webcontext}/crmdp/dealer/member/checkCarPlateRule',
				contentType : "application/json;charset=utf-8",
				data:JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val(),'carPlate':$("#"+o).val()}),
				async : false,
				dataType : 'json',
				success : function(data) {
					if(data){
						$.ajax({
							type:'POST',
							url: '${webcontext}/crmdp/dealer/member/checkCarPlateForMember',
							contentType : "application/json;charset=utf-8",
							async: true,
							data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
								,'carPlate':$("#"+o).val(),'id':$("#memberid").val()}),
							dataType : 'json',
							success : function(data) {
								if(data.carplatesExist){
									jAlert("该车牌已存在,请重新修改！", "提示信息",function(){
										window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
											+ data.mobilePhone
											+ "&memberName="+ data.memberName
											+ "&carPlate="+ data.carPlate
											+ "&id="+ data.id;
									});
								}else{
									jConfirm("您修改后的车牌号为  <font style='color: red;font-weight: bolder;'>"+document.getElementById(o).value+
									"</font>，<br><font style='color: red;font-weight: bolder;'>只可修改${memberResultsDto.updateCarplateTimes}次！</font>确认修改吗？", "提示信息", function(result){
										if(result){
											$.ajax({
												type:'POST',
												url: '${webcontext}/crmdp/dealer/member/updateCarPlateForMember',
												async: true,
												contentType : "application/json;charset=utf-8",
												data : JSON.stringify({'mobilePhone':$("#mobilePhone").val(),'memberName':$("#memberName").val()
													,'carPlate':$("#"+o).val(),'id':o,'memberid':$("#memberid").val()}),
												dataType : 'json',
												success : function(data) {
													if(data.updateCarplateSuccess){
														jAlert("车牌修改成功！","提示信息",function(){
														window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
															+ data.mobilePhone
															+ "&memberName="+ data.memberName
															+ "&carPlate="+ data.carPlate
															+ "&id="+ data.memberId;
														});
													}else if(data.greaterThanUpdateCarplateTimes){
														jAlert("车牌修改次数超过最大限制，不可以修改！","提示信息",function(){
															window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
																+ data.mobilePhone
																+ "&memberName="+ data.memberName
																+ "&carPlate="+ data.carPlate
																+ "&id="+ data.memberId;
														});
													}else{
														jAlert("车牌修改失败，请重新操作！","提示信息",function(){
															window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
																+ data.mobilePhone
																+ "&memberName="+ data.memberName
																+ "&carPlate="+ data.carPlate
																+ "&id="+ data.memberId;
														});
													}
											    }
											});
										}else{
											window.location.href="${webcontext}/crmdp/dealer/purchase/toUpdateMemberPage?mobilePhone="
												+ data.mobilePhone
												+ "&memberName="+ data.memberName
												+ "&carPlate="+ data.carPlate
												+ "&id="+ data.id;
										}
									});
								}
							}
						});
					}else{
						jAlert("车牌不符合规范，请重新输入！","提示信息");
					}
				}
			});
		}
		
		function closeLayer(){
			//当你在iframe页面关闭自身时
			var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
			parent.layer.close(index); //再执行关闭 	
			//window.parent.search();
		}
</script>

<style>
html,body {
	font: 14px "Microsoft YaHei", "Microsoft YaHei", Verdana, Arial,
		sans-serif;
	color: #000;
	height:150px;
	width:500px;
}
.labelShow {
	float: left;
	width: 20%;
	line-height: 30px;
	text-align: right;
}

.inputDiv {
	float: left;
	width: 50%;
}

.main {
	position: relative;
	margin: 0 auto;
	padding: 10px 10px;
	width: 570px;
	height: 350px;
}
</style>
<body>
	<div class="main">
		<form:form class="form-horizontal" id="edit_purchase" method="post"
			action="${webcontext}/crmdp/dealer/masterdata/updateDealerAndCarplate" modelAttribute="memberResultsDto" role="form"
			enctype="multipart/form-data">
			<input type="hidden" id="memberid" value="${memberResultsDto.id}">
			<div id="tell" class="form-group has-feedback">
				<label for="mobilePhone" class="col-sm-2 control-label labelShow">手机</label>
				<div class="col-sm-5 inputDiv">
					<form:input type="text" path="mobilePhone" class="form-control" id="mobilePhone"
						check-type="mobile" value="${memberResultsDto.mobilePhone}" disabled="true"/>
				</div>
				<div>
					<a id="toUpdateMobile">&nbsp;修改</a>
					<a id="updateMobile"></a>
					<span id="newMobile" style="color:green;"></span>
				</div>
				<span id="mobilePhone_valierr" class="help-block"></span>
			</div>
			<div class="form-group">
				<label for="memberName" class="col-sm-2 control-label labelShow">姓名</label>
				<div class="col-sm-5 inputDiv">
					<form:input type="text" class="form-control" id="memberName" value="${memberResultsDto.memberName}" 
						 path="memberName" check-type="required" disabled="true" required-message="请填写姓名" />
				</div>
				<div>
					<a id="toUpdateName">&nbsp;修改</a>
                   	<a id="updateName"></a>
                   	<span id="newName" style="color:green;"></span>
				</div>
			</div>
			<core:forEach items="${carPlateInfoWithoutPackage}" var="carPlateInfo">
				<div id="chepai" class="form-group has-feedback">
					<label for="carPlate" class="col-sm-2 control-label labelShow">车牌</label>
					<div class="col-sm-5 inputDiv">
						<form:input type="text" class="form-control" id="${carPlateInfo.id}"
							 value="${carPlateInfo.carPlate}" name="carPlates" disabled="true" path="carPlates" check-type="required" required-message="请填写车牌" />
					</div>
					<div>
						&nbsp;<a id="remove${carPlateInfo.id}" onclick="removeById(${carPlateInfo.id});">删除</a>
                		&nbsp;<a id="toUpdate${carPlateInfo.id}" onclick="toUpdateCarplate(${carPlateInfo.id});">修改</a>
                		<a id="update${carPlateInfo.id}" onclick="updateCarplate(${carPlateInfo.id});"></a>
                		<span id="updateSuccess" style="color:green;"></span>
					</div>
				</div>
			</core:forEach>
			<core:forEach items="${carPlateWithPackage}" var="carPlateWithPackage">
				<div id="chepai" class="form-group has-feedback">
					<label for="carPlate" class="col-sm-2 control-label labelShow">车牌</label>
					<div class="col-sm-5 inputDiv">
						<form:input type="text" class="form-control" id="${carPlateWithPackage.id}"
							 value="${carPlateWithPackage.carPlate}" disabled="true" name="carPlates" path="carPlates" check-type="required" required-message="请填写车牌" />
					</div>
					<div>
<%-- 						&nbsp;<a id="remove${carPlateWithPackage.id}" onclick="removeById(${carPlateWithPackage.id});">删除</a> --%>
                		&nbsp;<a id="toUpdate${carPlateWithPackage.id}" onclick="toUpdate(${carPlateWithPackage.id});">修改</a>
                		<a id="update${carPlateWithPackage.id}" onclick="update(${carPlateWithPackage.id});"></a>
                		<span id="updateSuccess" style="color:green;"></span>
					</div>
				</div>
			</core:forEach>
				<div id="tianjiachepai" class="form-group has-feedback">
					<label for="carPlate" class="col-sm-2 control-label labelShow">添加车牌</label>
					<div class="col-sm-5 inputDiv">
						<input type="text" class="form-control" id="carPlate" check-type="required" required-message="请填写车牌" />
					</div>
					<div>
						<a id="add">添加</a>
			      		<span id="span_msg"></span>
					</div>
					<span id="carPlate_valierr" class="help-block"></span>
				</div>
				<div class="col-sm-offset-2 col-sm-10 text-center">
					<button type="button" class="btn btn-success "
						onClick="closeLayer()">
						<i class="glyphicon glyphicon-share-alt" style="color: #FFFFFF"></i>返回
					</button>
				</div>
		</form:form>
	</div>
</body>
</html>